// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SimulatedSimpleModbusTCPConnection.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Test.Robotics.Hardware.Modbus
{
    using System;
    using System.Net.Sockets;
    using System.Runtime.Serialization;

    using Microsoft.Robotics.Hardware.Modbus;

    /// <summary>
    /// A simulated partial implementation of ModbusTCP http://en.wikipedia.org/wiki/Modbus implementing 
    /// function codes 4 (Read Input Registers) and 16 (Write Multiple Registers) for a single 
    /// remote device.
    /// </summary>
    public class SimulatedSimpleModbusTCPConnection : SimpleModbusTCPConnection
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SimulatedSimpleModbusTCPConnection"/> class.
        /// </summary>
        public SimulatedSimpleModbusTCPConnection()
        {
            this.ExpectedSendPacket = new byte[0];
        }

        /// <summary>
        /// Gets or sets the bytes that are expected when <c>SendPacket</c> is called.
        /// </summary>
        public byte[] ExpectedSendPacket { get; set; }

        /// <summary>
        /// Mock version of ReadPacket
        /// </summary>
        /// <param name="readBuffer">Not used.</param>
        /// <returns>Returns 0</returns>
        protected override int ReadPacket(byte[] readBuffer)
        {
            return 0;
        }

        /// <summary>
        /// Test version of <c>SendPacket</c>.  Throws if <c>packet</c> isn't identical to <c>ExpectedSendPacket</c>.
        /// </summary>
        /// <param name="packet">Packet to be sent.</param>
        /// <param name="bytesToWrite">Number of bytes to send.</param>
        protected override void SendPacket(byte[] packet, int bytesToWrite)
        {
            if (bytesToWrite != this.ExpectedSendPacket.Length)
            {
                throw new ApplicationException("Packet to send is not of the expected length");
            }

            // Check that all values in the packet are same as expected except for transaction ID (1st two bytes)
            for (int i = 2; i < bytesToWrite; ++i)
            {
                if (packet[i] != this.ExpectedSendPacket[i])
                {
                    throw new ApplicationException("Byte to send at position " + i + " is " + packet[i] + "; Expected value of " + this.ExpectedSendPacket[i]);
                }
            }
        }
    }
}
